package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type UserPermissionEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	Name   string   // 模板名称
	Params []string // 设备ID
}

type userPermissionPo struct {
	Id         int64    `json:"id" db:"id"`                  // 对象id
	CreateTime int64    `json:"createTime" db:"create_time"` // 创建时间
	UpdateTime int64    `json:"updateTime" db:"update_time"` // 更新时间
	Name       string   `json:"name" db:"name"`              // 设备厂商名称
	Params     []string `json:"params" db:"permission"`      // 扩展参数
}

func (e *UserPermissionEntity) Type() string {
	return "UserPermissionEntity"
}

func (e *UserPermissionEntity) TableName() string {
	return "tb_user_permission"
}

func (e *UserPermissionEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *UserPermissionEntity) Instance() *UserPermissionEntity {
	return &UserPermissionEntity{
		BaseEntity: new(commEntity.BaseEntity),
		Params:     make([]string, 0),
	}
}

func (e *UserPermissionEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *UserPermissionEntity) NewPo() interface{} {
	return &userPermissionPo{
		Params: make([]string, 0),
	}
}

func (e *UserPermissionEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *UserPermissionEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.Name)
	return p
}

func (e *UserPermissionEntity) BuildByJson(jsn *string) error {
	var po userPermissionPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *UserPermissionEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *UserPermissionEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*userPermissionPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.Params = po.Params
	ptr.Name = po.Name
}

func (e *UserPermissionEntity) BuildToPo() interface{} {
	ptr := e
	po := userPermissionPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.Params = ptr.Params
	po.Name = ptr.Name

	return &po
}
